<?php
namespace app\controller;

use app\model\Article;
use app\model\Banner;
use app\model\Column;
use app\model\Site;
use think\Controller;
use think\Db;

class Index extends Controller
{
    protected $site_id;
    protected $seo_info;
    public function initialize()
    {
        $this->site_id = 8;
        $res = Site::alias('a')->leftJoin('company b','a.company_id = b.id')->where('a.id',$this->site_id)->field('a.site_url,a.company_id,a.language_id,a.keyword,a.description,a.wx_qrcode,a.qq,a.address,a.phone,a.email,a.fax,a.hlogo,a.flogo,a.public_img,a.content,a.record,b.company_name')->find();
//        var_dump($res);
        $pathinfo = '/' . $this->request->pathinfo();
        if($pathinfo == '' || $pathinfo == '/index/index'){
            $this->seo_info = [
                'title' => $res['company_name'],
                'keyword' => $res['keyword'],
                'description' => $res['description']
            ];
        }else if($pathinfo == '/index/newsDetails'){

        }else{
            $seo = Column::where('url',$pathinfo)->field('title,keyword,description,seo_title,seo_keyword,seo_description')->find();
            $this->seo_info = [
                'title' => $res['company_name'] . '-' . ($seo['seo_title'] ? $seo['seo_title'] : $seo['title']),
                'keyword' => ($seo['seo_keyword'] ? $seo['seo_keyword'] : $seo['keyword']),
                'description' => $seo['seo_description'] ? $seo['seo_description'] : $seo['description']
            ];
        }
        $this->assign('seo_info',$this->seo_info);
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $banner = Banner::where('site_id',$this->site_id)->field('id,img,url,title,description')->limit(5)->order('id','desc')->select();
        $product = Column::alias('a')->leftJoin('column b','a.pid = b.id')->where(['b.title'=>'产品中心','a.site_id'=>$this->site_id])->field('a.url,a.title,a.indexImg')->limit(5)->select();
        $apply = Column::alias('a')->leftJoin('column b','a.pid = b.id')->field('a.title,a.indexImg,a.url')->where(['b.title'=>'应用中心','a.site_id'=>$this->site_id])->select();
        $news = Article::alias('a')->leftJoin('column b','a.column_id = b.id')->where(['b.title'=>'新闻中心','a.site_id'=>$this->site_id])->field('a.id,a.title,a.short_title,a.keyword,a.description,a.thumbnail')->limit('4')->order('a.id','desc')->select();
        $this->assign([
            'banner'=>$banner,
            'product'=>$product,
            'apply'=>$apply,
            'news' =>$news
        ]);
        return $this->fetch();
    }

    /**
     * 产品中心
     */
    public function product()
    {
        $banner = Column::where('site_id',$this->site_id)->where('title','产品中心')->value('img');
        $list = Column::alias('a')->leftJoin('column b','a.pid = b.id')->where(['a.site_id'=>$this->site_id,'b.title'=>'产品中心'])->field('a.id,a.title,a.bckimg,a.url,a.sort')->order('sort','desc')->select()->toArray();
        foreach($list as $k => $v){
            $list[$k]['child'] = Column::where('pid',$v['id'])->field('title,url')->select()->toArray();
        }
        $this->assign([
            'banner'=>$banner,
            'list'=>$list
        ]);
        return $this->fetch();
    }

    /**
     * 应用中心
     * @return mixed
     */
    public function apply()
    {
        $list = Column::alias('a')->leftJoin('column b','a.pid = b.id')->where(['a.site_id'=>$this->site_id,'b.title'=>'应用中心'])->field('a.id,a.title,a.img,a.url,a.sort')->order('sort','desc')->select()->toArray();
        $i = 0;
        foreach($list as $k => $v){
            $i++;
            if($i <= 2){
                $list[$k]['child'] = Column::field('title,img,bckimg,indexImg')->where('pid',$v['id'])->select()->toArray();
            }else{
                $list[$k]['child'] = [];
            }

        }
        $this->assign([
            'list'=>$list
        ]);
        return $this->fetch();
    }

    /**
     * 服务支持
     * @return mixed
     */
    public function serve()
    {
        $list = Column::alias('a')->rightJoin('column b','a.id = b.pid')->field('b.id,b.title,b.url,b.img,b.bckimg,b.indexImg,b.sort')->where(['b.site_id'=>$this->site_id,'a.title'=>'服务支持'])->order('sort','desc')->select();
        $this->assign([
            'list'=>$list
        ]);
        return $this->fetch();
    }

    /**
     * 样品获取
     * @return mixed
     */
    public function sample()
    {
        return $this->fetch();
    }

    /**
     * 关于我们
     * @return mixed
     */
    public function about()
    {
        return $this->fetch();
    }

    /**
     *
     */
    public function aboutCertify()
    {
        return $this->fetch();
    }

    public function aboutCulture()
    {
        return $this->fetch();
    }

    public function aboutContact()
    {
        return $this->fetch();
    }

    public function aboutDevelopment()
    {
        return $this->fetch();
    }

    public function aboutNews()
    {
        return $this->fetch();
    }

    public function aboutPatent()
    {
        return $this->fetch();
    }

    public function aboutQualification()
    {
        return $this->fetch();
    }

    public function aboutRecruit()
    {
        return $this->fetch();
    }
    /**
     * 新闻中心
     */
    public function newsDetails($id)
    {
        return $this->fetch();
    }

    public function getcity()
    {
        $a = json_decode(file_get_contents('https://cdn.cloudpnr.com/adapayresource/documents/Adapay%E7%9C%81%E5%B8%82%E7%BC%96%E7%A0%81%EF%BC%88%E5%9B%9B%E4%BD%8D%EF%BC%89.json'),true);
        foreach($a as $k => $v){
            db('adapay_bank_region')
                ->insert([
                    'code'=>$v['value'],
                    'name'=>$v['title'],
                    'pcode'=>0
                ]);
            if(!empty($v['cities'])){
                foreach($v['cities'] as $k2 => $v2){
                    db('adapay_bank_region')
                        ->insert([
                            'code'=>$v2['value'],
                            'name'=>$v2['title'],
                            'pcode'=>$v['value']
                        ]);
                }
            }
        }



    }

}
